package cn.parllay.dingdangproject.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;

import cn.parllay.dingdangproject.utils.DensityUtil;
import cn.parllay.dingdangproject.utils.GlideUtils;
import cn.parllay.dingdangproject.bean.notice.Notice;
import cn.parllay.dingdangproject.R;
import cn.parllay.dingdangproject.activity.CustomerInfoActivity;


/**
 * Created by sunny on 2017/8/17.
 */
public class CenterWindow {

    private final static int ANIM_DURATION = 600;
    public static int viewWidth;
    public static int viewHeight;

    private Context mContext;
    private View mCenterWindow;
    private WindowManager windowManager;
    private LinearLayout linearLayout;
    private float downX;
    private float downY;
    private WindowManager.LayoutParams params;

    private Button notice_receive;
    private ImageView notice_headImage;
    private TextView notice_color_size;
    private TextView notice_style;

    private Notice mNotice;


    //标识弹窗是否是显示状态
    private boolean isShow = false;

    public CenterWindow(Context context) {
        mContext = context.getApplicationContext();
    }

    public synchronized void popupCenterWindow(Notice notice) {
        mNotice = notice;
        if (!isShow) {
            setUpCenterWindow();

            addToWindowManager();

            setCenterWindowInAnim();

            initEvent();

        }
        bindData(notice);

    }

    private void initEvent() {
        notice_receive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //删除弹窗
                animDismiss();

                //进入用户详细信息页
                startToVipDetailActivity();
            }
        });
    }


    //跳转到顾客详细信息页
    private void startToVipDetailActivity() {
        CustomerInfoActivity.actionStart(mContext, mNotice.getContactId());

    }


    //绑定数据
    private void bindData(Notice notice) {

        if (notice == null) {
            return;
        }
        GlideUtils.loadCircleImage(mContext, GlideUtils.checkUrl(notice.getHeadImage()), notice_headImage);

        notice_style.setText(notice.getStyleName().isEmpty() ? "---" : notice.getStyleName());
        String colorName = notice.getColorName().isEmpty() ? "---" : notice.getColorName();
        String sizeName = notice.getSizeName().isEmpty() ? "---" : notice.getSizeName();
        notice_color_size.setText(colorName + "/" + sizeName);
    }

    /**
     * 为mHeaderToastView添加进入动画
     */
    private void setCenterWindowInAnim() {
        ObjectAnimator a = ObjectAnimator.ofFloat(mCenterWindow, "alpha", 0, 1);
        a.setDuration(ANIM_DURATION);
        a.start();
    }

    private void setUpCenterWindow() {

        linearLayout = new LinearLayout(mContext);
        linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.blue1));
        LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(llParams);

//        mCenterWindow = View.inflate(mContext, R.layout.window_center_vip_notice, linearLayout);
        mCenterWindow = LayoutInflater.from(mContext).inflate(R.layout.window_center_vip_notice, linearLayout);

        notice_receive = (Button) mCenterWindow.findViewById(R.id.notice_receive);
        notice_headImage = (ImageView) mCenterWindow.findViewById(R.id.notice_headImage);
        notice_color_size = (TextView) mCenterWindow.findViewById(R.id.notice_color_size);
        notice_style = (TextView) mCenterWindow.findViewById(R.id.notice_style);
    }

    private void addToWindowManager() {
        windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        //为mHeaderToastView添加parent使其能够展示动画效果

        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
//        wmParams.gravity = Gravity.CENTER_HORIZONTAL;
//        wmParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        wmParams.gravity = Gravity.CENTER;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.format = PixelFormat.TRANSLUCENT;
        wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        windowManager.addView(linearLayout, wmParams);

        isShow = true;
    }

    /**
     * HeaderToast消失动画
     */
    private void animDismiss() {

        if (null == linearLayout || null == linearLayout.getParent()) {
            //如果linearLayout已经被从wm中移除，直接return
            return;
        }

        ObjectAnimator a = ObjectAnimator.ofFloat(mCenterWindow, "alpha", 1, 0);
        a.setDuration(ANIM_DURATION);
        a.start();

        a.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                dismiss();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    /**
     * 移除弹窗
     */
    public void dismiss() {
        if (null != linearLayout && null != linearLayout.getParent()) {
            windowManager.removeView(linearLayout);
            isShow = false;
        }
    }

    public boolean isShow() {
        return isShow;
    }

    public void setParams(WindowManager.LayoutParams params) {
        this.params = params;
    }

//    @Override
//    public boolean onTouch(View v, MotionEvent event) {
//        switch (event.getAction()){
//            case MotionEvent.ACTION_DOWN:
//                downX = event.getRawX();
//                downY = event.getRawY();
//                break;
//            case MotionEvent.ACTION_MOVE:
//                float currentX = event.getRawX();
//                float currentY = event.getRawY();
//                if((currentX - downX ) >= 40 || (currentY - downY ) >= 10){
//                    animDismiss();
//                }
//                break;
//        }
//        return true;
//    }

    private void addTextViewsByTags(LinearLayout linearLayout, String tagString) {

        linearLayout.removeAllViews();

        if (tagString == null || "".equals(tagString)) {
            return;
        }
        List<String> tags = splitTags(tagString);
        for (int i = 0; i < tags.size(); i++) {
            TextView textView = new TextView(mContext);
            textView.setTextColor(mContext.getResources().getColor(R.color.white));
            textView.setBackgroundResource(R.drawable.shape_tags_normal_white);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i > 0) {
                layoutParams.leftMargin = DensityUtil.dip2px(mContext, 5);
            }
            int padding = DensityUtil.dip2px(mContext, 3);
            textView.setPadding(padding, padding, padding, padding);
            textView.setText(tags.get(i));
            linearLayout.addView(textView, layoutParams);

        }
    }

    private List<String> splitTags(String tags) {

        String[] split = tags.split("\\^");
        return Arrays.asList(split);
    }
}
